package com.mobile.collect.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.mobile.collect.R;
import com.mobile.collect.beans.ExportResultInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.dialogs.SubmitTipDialog;
import com.mobile.collect.present.UploadPresent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.BaseListActivity;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;

/**
 * 类名: ExportListActivity
 * 作者: 李学伟
 * 日期: 2020年09月30日 10:25
 * 描述:
 */
public class UploadActivity extends BaseListActivity<ExportResultInfo, UploadPresent> {

    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.submitTv)
    Button submitTv;

    private Long exportInfoId;
    private String exportTime;

    @Override
    public void initView() {
        super.initView();
        titleTv.setText("上传");
        submitTv.setText("上传");
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadmore(false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        selectList = new ArrayList<>();
        List<ExportResultInfo> list = EntityManager.getInstance().queryExportResultInfo(exportInfoId,exportTime);
        onSuccess(list);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_exportlist;
    }

    @Override
    public int getTitleLayoutResId() {
        return 0;
    }

    List<ExportResultInfo> selectList = new ArrayList<>();

    @Override
    public BaseRecyclerAdapter<ExportResultInfo> getAdapter() {
        return new BaseRecyclerAdapter<ExportResultInfo>(this) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_upload;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, final int position) {
                super.bindData(holder, position);
                final ExportResultInfo info = getItemByPosition(position);
                holder.setText(R.id.deviceNameTv, info.getDeviceName());
                if (selectList.contains(info)) {
                    holder.setImageResDrawable(R.id.selectIv, R.mipmap.select1);
                } else {
                    holder.setImageResDrawable(R.id.selectIv, R.mipmap.select0);
                }
                if (info.getHadUploaded()) {
                    holder.setVisibility(R.id.statusTv, View.VISIBLE);
                } else {
                    holder.setVisibility(R.id.statusTv, View.INVISIBLE);
                }
            }
        };
    }

    @Override
    public void itemClick(int position) {
        final ExportResultInfo info = mBaseAdapter.getItemByPosition(position);
        if (selectList.contains(info)) {
            selectList.remove(info);
        } else {
            selectList.add(info);
        }
        checkSlelectCount();
    }

    private void checkSlelectCount() {
        if (selectList.size() == mBaseAdapter.getItemCount() && mBaseAdapter.getItemCount() != 0) {
            rightIv.setImageResource(R.mipmap.allselect1);
        } else {
            rightIv.setImageResource(R.mipmap.allselect0);
        }
        if (selectList.size() != 0) {
            submitTv.setVisibility(View.VISIBLE);
        } else {
            submitTv.setVisibility(View.GONE);
        }
        mBaseAdapter.notifyDataSetChanged();
    }


    @Override
    public void getDataFromServer() {

    }

    @Override
    public void getIntentData() {
        exportInfoId = getIntent().getLongExtra("exportInfoId", 0l);
        exportTime = getIntent().getStringExtra("exportTime");
    }

    @Override
    public UploadPresent newP() {
        return new UploadPresent();
    }

    public static void lunch(Context context, Long exportInfoId, String exportTime) {
        Intent intent = new Intent(context, UploadActivity.class);
        intent.putExtra("exportInfoId", exportInfoId);
        intent.putExtra("exportTime", exportTime);
        context.startActivity(intent);
    }

    public static void lunch(Context context) {
        Intent intent = new Intent(context, UploadActivity.class);
        context.startActivity(intent);
    }

    @OnClick({R.id.rightIv, R.id.submitTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rightIv:
                if (selectList.size() == mBaseAdapter.getItemCount() && selectList.size() != 0) {
                    selectList.clear();
                } else {
                    selectList.clear();
                    selectList.addAll(mBaseAdapter.getmList());
                }
                checkSlelectCount();
                break;
            case R.id.submitTv:
                if (selectList.size() == 0) {
                    showToast("请选择上传的信息");
                    break;
                }

                boolean hadUploaded = false;
                for (ExportResultInfo exportResultInfo : selectList) {
                    hadUploaded = exportResultInfo.getHadUploaded();
                    if (hadUploaded) {
                        break;
                    }
                }
                if (hadUploaded) {
                    showRepeatDialog();
                } else {
                    showUploadDialog();
                }
                break;
        }
    }


    public void uploadSuccess() {
        mBaseAdapter.notifyDataSetChanged();
        disLoadDialog();
    }

    public void showRepeatDialog() {
        SubmitTipDialog tipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
            @Override
            public void submitClick() {
                getP().startUpload(selectList);
            }

            @Override
            public void cancelClick() {

            }
        }, "");
        tipDialog.display("提示", "所选数据将同步到服务器，是否同步?", "取消", "确定");
    }

    public void showUploadDialog() {
        SubmitTipDialog tipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
            @Override
            public void submitClick() {
                getP().startUpload(selectList);
            }

            @Override
            public void cancelClick() {

            }
        }, "");
        tipDialog.display("提示", "所选数据将同步到服务器，是否同步?", "取消", "确定");
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }

}
